package com.tmsps.common.bean;

import com.alibaba.fastjson.JSONObject;
import com.tmsps.common.exception.ServiceException;
import com.tmsps.common.util.jwt.JWTUtil;
import com.tmsps.common.util.result.ResponseCodeEnum;
import com.tmsps.ne4spring.page.Page;
import com.tmsps.ne4spring.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author jiangtao
 */
@Slf4j
public class BaseAction {

    /**
     * 获取当前登录账户id
     *
     * @return userId
     */
    public Long getCurrentUserId(HttpServletRequest request){
        Object userId = this.getTokenValue(request, Constant.TOKEN_KEY_USER_ID);
        return (Long) userId;
    }

    /**
     * 获取当前登录账户类型
     *
     * @return userType
     */
    public Integer getCurrentUserType(HttpServletRequest request){
        Object userType = this.getTokenValue(request, Constant.TOKEN_KEY_USER_TYPE);
        return (Integer) userType;
    }

    /**
     * 获取当前登录账户所属平台
     *
     * @return endpoint
     */
    public String getCurrentUserEndpoint(HttpServletRequest request){
        Object endpoint = this.getTokenValue(request, Constant.TOKEN_KEY_USER_ENDPOINT);
        return (String) endpoint;
    }

    /**
     * 获取当前回话 token全部信息
     *
     * @return allValue
     */
    public Map<String, Object> getTokenAllValue(HttpServletRequest request){
        String token = request.getHeader(Constant.AUTHORIZATION);
        checkToken(token);
        return JWTUtil.getAllValue(token);
    }

    /**
     * 获取查询参数
     */
    protected JSONObject getSrh(HttpServletRequest request){
        Enumeration<String> names = request.getParameterNames();
        JSONObject srh = new JSONObject();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            if (name.startsWith("srh_")) {
                String key = name.substring(4);
                String val = request.getParameter(name);
                srh.put(key, val);
            }
        }
        return srh;
    }

    /**
     * 获取分页数据
     */
    public Page getPage(HttpServletRequest request) {
        Page page = new Page();
        Integer pageNumber = ChkUtil.getInteger(request.getParameter("pageNumber"));
        Integer pageSize = ChkUtil.getInteger(request.getParameter("pageSize"));
        if (pageNumber > 0) {
            page.setPageNumber(pageNumber);
        }
        if (pageSize > 0) {
            page.setPageSize(pageSize);
        }
        return page;
    }

    /**
     * 获取 token 中的值
     * @param request
     * @return
     */
    private Object getTokenValue(HttpServletRequest request, String key){
        String token = request.getHeader(Constant.AUTHORIZATION);
        checkToken(token);
        return JWTUtil.getValue(token, key);
    }

    /**
     * 检验 token
     * @param token
     */
    private void checkToken(String token) {
        boolean verifier = JWTUtil.verifier(token);
        if(!verifier){
            throw new ServiceException(ResponseCodeEnum.EX_USER_REG_AUTHORITY_EXPIRED);
        }
    }
}
